<?php
/**
 * 重庆柯一网络有限公司
 * 遵循MT协议，开源并可商业使用，没有任何限制
 * @Author:cqkyi
 * @Date: 2020/9/27 11:20
 * 官方惟一地址：www.cqkyi.com
 */

namespace app\apiadmin\controller;


use app\common\model\admin\RoleMenuModel;
use app\common\model\admin\RoleModel;
use app\common\utils\Res;

class Role extends AuthController
{




    /**
     * @return \think\response\Json
     * 添加角色
     */
    public function add(){
        $param = $this->request->post();
        $rules = $this->request->post('rules/a');
        $param['create_by']=$this->request->username;
        $result = RoleModel::add($param,$rules);
        return Res::jsonResult($result);
    }

    public function index(){
        $param = $this->request->post();
        $limit =(int)$param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;
        $keyword = $param['keyword'];
        $map=[];
        if($keyword){
            $map[] = ['role_name','like','%'.$keyword.'%'];
        }
        $res = RoleModel::pageMap($map, $offset, $limit);
        $total = RoleModel::pageMapCount($map);
        $data=[
            'list'=>$res,
            'total'=>$total
        ];
        return Res::ok("查询成功",$data);
    }


    public function selectRoleMenuId($id){
       $result = RoleMenuModel::findByRoleId($id);
       $data=[];
       foreach ($result as $k=>$v){
           $data[$k]=$v['menu_id'];
       }
       return Res::ok("查询成功",$data);
    }

    public function edit(){
        $param = $this->request->post();
        $rules = $this->request->post('rules/a');
        $param['update_by']=$this->request->username;
        $param['update_time']=date('Y-m-d H:i:s',time());
        $result = RoleModel::edit($param,$rules);
        return Res::jsonResult($result);
    }

    public function del($id){
        $result = RoleModel::del($id);
        return Res::jsonResult($result);
    }

    public function changeStatus($id){
        $result = RoleModel::changeStatus($id);
        return Res::jsonResult($result);
    }
    public function changeOrder()
    {
        $param = $this->request->post();
        $result = RoleModel::changeOrder($param);
        return Res::jsonResult($result);
    }

    public function batchRemove(){
        $param = $this->request->post("ids/a");
        $result = RoleModel::batchRemove($param);
        return Res::jsonResult($result);
    }


    public function listRole(){
        $result = RoleModel::findRoleAll();
        return Res::ok("查询成功",$result);
    }
}